package cn.sunny360.messsage.impl.json;

import cn.sunny360.messsage.MsgResult;
import com.alibaba.fastjson.JSONObject;

import java.nio.charset.StandardCharsets;

public class JsonMsgResult implements MsgResult<JsonMsgResult> {
    private JSONObject content;

    public JsonMsgResult(){
        content = new JSONObject();
    }

    public JsonMsgResult(String json){
        content = JSONObject.parseObject(json);
    }

    public JsonMsgResult(JSONObject jsonObject) {
        content = jsonObject;
    }

    @Override
    public String toJson() {
        return content.toJSONString();
    }

    @Override
    public JsonMsgResult parseJson(String s) {
        content = JSONObject.parseObject(s);
        return this;
    }

    @Override
    public byte[] toByteArray() {
        return content.toJSONString().getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public JsonMsgResult parseByteArray(byte[] bytes) {
        content = JSONObject.parseObject(new String(bytes, StandardCharsets.UTF_8));
        return this;
    }


    @Override
    public int getStatus() {
        return content.getIntValue("status");
    }

    @Override
    public String getMsg() {
        return content.getString("msg");
    }

    @Override
    public String getData() {
        return content.getString("data");
    }

    @Override
    public MsgResult setStatus(int status) {
        content.put("status", status);
        return this;
    }

    @Override
    public MsgResult setMsg(String msg) {
        content.put("msg", msg);
        return this;
    }

    @Override
    public MsgResult setData(String data) {
        content.put("data", data);
        return this;
    }
}
